package game;

import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;


public abstract class MusicPlayer extends Thread{
	protected DataLine out;
	protected float volume;
	
	public MusicPlayer(float v){
		volume = v;
	}
	
	private <E> void out(E m){
		System.out.println(m.toString());
	}
	
	public void finish(){}
	
	public void setVolume(float v){
		try{
			if(out.getControls().length > 1){
				if(out.getControls()[0] instanceof FloatControl){
					if(v <= .01){
						((FloatControl)out.getControls()[0]).setValue(-80);
					}else{
					    // TODO - Bad, but it fixes the crash when volume is @ 100% 
					    if(40*v-30 > 6)
					        ((FloatControl)out.getControls()[0]).setValue(6f);
					    else
					        ((FloatControl)out.getControls()[0]).setValue(40*v-30);
					}
				}
			}
		}catch(NullPointerException e){}
	}
	
	public float getVolume(){
		if(((FloatControl)out.getControls()[0]).getValue() == -80){
			return 0;
		}else{
			return (float)(((FloatControl)out.getControls()[0]).getValue()+30)/40;
		}
	}
	
	public void setRate(float v){
		try{
			if(out.getControls().length > 3){
				if(out.getControls()[3] instanceof FloatControl){
					((FloatControl)out.getControls()[3]).setValue(32000*v+16000);
				}
			}
		}catch(NullPointerException e){}
	}
	
	public float getRate(){
		return (float)(((FloatControl)out.getControls()[0]).getValue()-16000)/32000;
	}
}
